﻿<%@ WebHandler Language="C#" Class="ListeningHandlerService" %>

using System;
using System.Web;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class ListeningHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================

    //添加听书内容
    public string Add_New_ListeningBook_Info()
    {
        string Title = HttpContext.Current.Request["Title"];//有声图书的说明标题
        string Description = HttpContext.Current.Request["Description"];//有声图书的说明描述信息
        string AnnouncerInfo = HttpContext.Current.Request["AnnouncerInfo"];//与录音者相关的一些信息。例如：某某家庭、某某教师、班级、原因等。
        HttpPostedFile file = HttpContext.Current.Request.Files["UploadFileInfo"];//获取上传文件，保存在服务器端文件的名称，以目录路径来存放。
        string TimeLength = HttpContext.Current.Request["TimeLength"];//录音文件的时间长度，通常以MP3格式来保存文件。
        string BookLinkPic = HttpContext.Current.Request["BookLinkPic"];//指定网上图片，以显示图书封面。
        string BookLinkInfo = HttpContext.Current.Request["BookLinkInfo"];//按行存放链接信息。例如有两本书对应，就用两行来存放链接。读取时，由前台循环处理。

        string fn = "";

        try
        {
            Helper.uploadFileToFolder(file, out fn);

            if (fn.Length == 0)
            {
                throw new Exception();
            }
        }
        catch (Exception ex)
        {
            return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "文件上传时发生错误！" + ex.Message).ToJson();
        }
        
        int ReturnValue = 0;
        string ReturnFieldValue = "";

        //下面开始调用信息保存
        bool b = XJX.Page.Listening.LB_Add_New_ListeningBook_Info(Title, Description, AnnouncerInfo, fn, TimeLength, BookLinkPic, BookLinkInfo, out ReturnValue, out ReturnFieldValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "有声图书添加成功!", ReturnFieldValue).ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "有声图书添加失败，请检查数据!", 99).ToJson();
                //删除已经上传的mp3文件。
                Helper.DeleteUploadFile_BackCall(fn);
                break;
            case 100:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "不能添加相同交流主题!", 100).ToJson();
                break;

        }
        return result;
    }

    //添加本版块评价
    public string Add_New_ListeningBookEval()
    {
        string ParentID = HttpContext.Current.Request["ListeningBookID"];//有声图书的一条记录编号
        string Content = HttpContext.Current.Request["Content"];//评价内容描述

        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"]; //fe2499d5-c9d5-4680-83b6-c25022118129
        string UID = fi.UID;

        //string LBID = "1";
        //string Content = "测试回复！";
        //string Egrade = "5";
        //string UID = "fe2499d5-c9d5-4680-83b6-c25022118129";
        
        int ReturnValue = 0;
        string ReturnFieldValue = "";

        //下面开始调用信息保存
        bool b = XJX.Page.Evaluate.Ev_Add_New_Evaluate("Listen",ParentID, UID, Content, out ReturnValue, out ReturnFieldValue);

        string result = "";
        switch (ReturnValue)
        {
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "回复成功!" ,1).ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "回复失败，请检查数据!", 99).ToJson();
                break;
            case 100:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "不能添加相同回复内容!", 100).ToJson();
                break;
        }
        return result;
    }

    //为某个有声图书点赞（再次点击则取消）
    public string Add_New_Credit()
    {
        string result;

        string ParentID = HttpContext.Current.Request["ListeningBookID"];

        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        bool b = XJX.Page.Evaluate.Ev_Add_New_Credit("Listen", ParentID, UID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "谢谢参与！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "参与 失败！").ToJson();
        }

        return result;
    }

    //删除一条有声图书记录
    public string Del_One_ListeningBook_From_LBID()
    {
        string result;
        string LBID = HttpContext.Current.Request["ListeningBookID"];

        bool b = XJX.Page.Listening.LB_Del_One_ListeningBook_From_LBID(LBID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "删除一条记录 成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "删除一条记录 错误(此主题下有回复)！").ToJson();
        }

        return result;
    }

    //删除一条评价内容
    public string Del_One_ListeningBookEval_From_ID()
    {
        string result;
        string ID = HttpContext.Current.Request["EvaluateID"];
        string UID = HttpContext.Current.Request["UID"];

        bool b = XJX.Page.Evaluate.Ev_Del_One_ContentEvaluate_From_ID(ID, UID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "删除一条回复 成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "删除一条回复 失败！").ToJson();
        }

        return result;
    }

    //修改一条有声图书信息
    public string Modify_One_ExchangeTheme_Info()
    {
        string LBID = HttpContext.Current.Request["ListeningBookID"];
        string Title = HttpContext.Current.Request["Title"];//有声图书的说明标题
        string Description = HttpContext.Current.Request["Description"];//有声图书的说明描述信息
        string AnnouncerInfo = HttpContext.Current.Request["AnnouncerInfo"];//与录音者相关的一些信息。例如：某某家庭、某某教师、班级、原因等。
        
        HttpPostedFile file = HttpContext.Current.Request.Files["UploadFileInfo"];//获取上传文件，保存在服务器端文件的名称，以目录路径来存放。
        string oldFile = HttpContext.Current.Request["oldFile"];//读取原来上传的mp3文件,如果没有新上传文件，则保存原来的文件名称。
        
        string TimeLength = HttpContext.Current.Request["TimeLength"];//录音文件的时间长度，通常以MP3格式来保存文件。
        string BookLinkPic = HttpContext.Current.Request["BookLinkPic"];//指定网上图片，以显示图书封面。
        string BookLinkInfo = HttpContext.Current.Request["BookLinkInfo"];//按行存放链接信息。例如有两本书对应，就用两行来存放链接。读取时，由前台循环处理。

        string fn = "";
        
        if (file == null)
        {
            if (oldFile == "")
            {
                return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "必需上传mp3声音文件！" ).ToJson();
            }
            
            fn = oldFile;
        }
        else
        {
            try
            {
                Helper.uploadFileToFolder(file, out fn);

                if (fn.Length == 0)
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "文件上传时发生错误！" + ex.Message).ToJson();
            }
        }
       
        string result;

        bool b = XJX.Page.Listening.LB_Modify_One_ListeningBook_Info(LBID, Title, Description, AnnouncerInfo, fn, TimeLength, BookLinkPic, BookLinkInfo);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "修改一条记录 成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "修改一条记录 错误！").ToJson();
        }

        return result;
    }

    //得到一本有声图书的内容
    public string Get_One_ListeningBook_Info_From_LBID()
    {
        string LBID = HttpContext.Current.Request["ListeningBookID"];

        return XJX.Page.Listening.Get_One_ListeningBook_Info_From_LBID(LBID);
    }

    //一般浏览用户或者管理员获取标题列表
    public string Search_ListeningBook_List()
    {
        string Title = HttpContext.Current.Request["Title"];
        string IsAll = HttpContext.Current.Request["IsAll"];//传入"ALL"字串，或者"PART"字串。管理员用ALL，一般用户用PART

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        //string Title = "小";
        //string IsAll = "ALL";
        //int PageIndex = 1;
        //int PageSize = 20;


        return XJX.Page.Listening.LB_Search_ListeningBook_List(Title, IsAll, PageIndex, PageSize);
    }

    //一般用户或者管理员获取某主题回复内容列表
    public string Search_ListeningBookEval_List()
    {
        string ParentID = HttpContext.Current.Request["ListeningBookID"];

        string IsAll = HttpContext.Current.Request["IsAll"];//传入"ALL"字串，或者"PART"字串。管理员用ALL，一般用户用PART

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.Evaluate.Ev_Search_ContentEvaluate_List("Listen",ParentID, IsAll, PageIndex, PageSize);
    }

    //屏蔽或显示一条
    public string ShieldORDisp_One_ListeningBook_By_LBID()
    {
        int ReturnValue;
        string ReturnFieldValue;

        string LBID = HttpContext.Current.Request["ListeningBookID"];

        string result;

        bool b = XJX.Page.Listening.LB_ShieldORDisp_One_ListeningBook_By_LBID(LBID, out ReturnValue, out ReturnFieldValue);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "操作成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "操作错误！").ToJson();
        }

        return result;

    }


    //置顶一条
    public string StickORCannel_One_ListeningBook_By_LBID()
    {
        int ReturnValue;
        string ReturnFieldValue;

        string LBID = HttpContext.Current.Request["ListeningBookID"];

        string result;

        bool b = XJX.Page.Listening.LB_StickORCannel_One_ListeningBook_By_LBID(LBID, out ReturnValue, out ReturnFieldValue);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "操作成功！" ).ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "操作错误！").ToJson();
        }

        return result;
    }
    
    

}